var express=require("express");

var bodyParser=require("body-parser");
var cookieParser=require("cookie-parser");
var app=express();
app.use(express.static("wwwroot"));

app.use(bodyParser.urlencoded({extended:false}));

app.use(cookieParser());


//app.use(require("./js/db.js"));//导入数据库写法错误
var db=require("./js/db.js");
var users= db.users;
var Carts= db.carts;
var template=require("./js/template.js");//导入art-template
app.engine(".html",template.__express);
app.set("view engine","html");
template.config("cache",false);
//首页页面接口
app.get("/",function(req,res){
	var data={
		title:"Panli-代购",
		name:req.cookies.name
	}
	res.render("index",data);//直接去views里面找
});
//退出登录接口
app.get("/logout",function(req,res){
	res.clearCookie("name");
	res.redirect("/");
})
//跳转到views/register（这是固定格式）
app.get("/register",function(req,res){
	res.render("register",{title:"注册-Panli代购"});
});

//注册接口
app.get("/login",function(req,res){
	res.render("login",{title:"登录-Panli代购"});
});

app.post("/register",function(req,res){
	console.log(req.body);
	req.body.createTime = new Date();
	
	users.find(req.body.username,function(err,result){
		console.log(result);
		if(!err){
			var flag;
			result.forEach(function(obj){
				if(req.body.username == obj.username){
					flag=true;
					return flag;
				}
//			for循环在nodejs中不能被遍历
			})			
			if(flag){
				res.json({error:1,msg:"用户名已被注册"});
			}else{
				var u = new users(req.body);
				u.save();
				res.json({error:0,msg:"注册成功"});	
			}
		}	
	})
})

app.post("/login",function(req,res){
	var name=req.body.username;
	var psw = req.body.password;
	users.find({username:name},function(err,result){
		if(!err){
			var flag=false;
			result.forEach(function(obj){
				if(name == obj.username){
					flag=true;
					if(psw == obj.password){
						res.cookie("name",name);
						res.json({err:0,msg:"登录成功"});
					}else{
						res.json({err:3,msg:"密码错误"});
					}
					return flag;
				}
			})
			if(!flag){
				res.json({err:1,msg:"该用户名不存在"});
			}	
		}else{
			res.json({err:2,msg:"服务器内部错误"});
		}
	})
})

app.get("/product",function(req,res){
	var data={
		title:"拼单购_代购拼单——panli代购",
		name:req.cookies.name
	}
	res.render("panli_list",data);
});

app.get("/product_detail",function(req,res){
	res.render("product_detail");
})

app.get("/product_detail_1",function(req,res){
	res.render("product_details");
})


//购物车接口
app.post("/carts",function(req,res){
	var name =req.cookies.name
	req.body.mark = name;
	var c = new Carts(req.body);
	c.save(function(err){
		if(!err){
			console.log(123);
			res.json({err:0,msg:"添加成功"});
		}
	});
})

app.get("/shoppingCar_login",function(req,res){
	var name =req.cookies.name	
	if (name) {
		Carts.find({mark:name},function(err,result){
			console.log(result);
			var sum=0;
			var all = result.forEach(function(obj){
				sum = sum+obj.price
				return sum;
			})
			var data={
				title:"购物车-Panli代购",
				name:name,
				result:result,
				sum:sum
			}
			res.render("shoppingCar_login",data)
		})
	} else{
		res.render("shoppingCar_unlogin")
	}
})


app.listen(8080,function(){
	console.log("hi boy");
})